package webgamemaker.Editor;

import java.util.ArrayList;

import webgamemaker.Common.Callback;
import webgamemaker.Common.TileSet;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

public class TileSetPanel extends TabSet  
{
	public ArrayList<Callback> selectCallback;
	public TileSetPicker selectedTileSetPicker;
	
	public TileSetPanel()
	{
		selectCallback = new ArrayList<Callback>();
	}
	
	public void addSelectCallback(Callback callback)
	{
		selectCallback.add(callback);
	}
	
	public TileSetTab addTileSet(TileSet tileset)
	{
		TileSetTab tab = new TileSetTab(tileset);
		setTabs(tab);
		return tab;
	}
	
	public TileSetTab findTab(TileSet tileset)
	{
		for(Tab tab : this.getTabs())
			if(((TileSetTab)tab).tilesetPicker.tileset == tileset)
				return (TileSetTab)tab;
		return null;
	}
	
	public class TileSetTab extends Tab
	{
		public TileSetPicker tilesetPicker;
		public TileSetTab(TileSet tileset)
		{
			super(tileset.name);
			tilesetPicker = new TileSetPicker(tileset);
			tilesetPicker.addSelectCallback(new Callback()
				{
					public void onCall(Object src) 
					{
						selectedTileSetPicker = (TileSetPicker)src;
						for(int i = 0; i < selectCallback.size(); i++)
							selectCallback.get(i).onCall(tilesetPicker);
					}
				});
			Canvas canvas = new Canvas();
			canvas.addChild(tilesetPicker);
			setPane(canvas);
			setCanClose(true);
		}
	}
}
